from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtGui import QResizeEvent
from PyQt5.QtWidgets import QVBoxLayout

from components import LineEditFrame
from qfluentwidgets import PushButton, TitleLabel, StateToolTip, InfoBar, CaptionLabel, TransparentToolButton, \
    FluentIcon, CardWidget


class WordFrameBase(CardWidget):
    returned = pyqtSignal()
    started = pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.initUI()

        self.stateToolTip = StateToolTip('', '', parent=self)
        self.stateToolTip.hide()

    def initUI(self):
        self.vBox = QVBoxLayout(self)
        self.vBox.setContentsMargins(10, 10, 10, 10)
        self.vBox.setSpacing(10)
        self.vBox.setAlignment(Qt.AlignTop | Qt.AlignVCenter)

        self.returnButton = TransparentToolButton(self)
        self.returnButton.setFixedSize(40, 40)
        self.returnButton.setIcon(FluentIcon.RETURN)
        self.titleLabel = TitleLabel()
        self.titleLabel.setAlignment(Qt.AlignCenter)
        self.openFileFrame = LineEditFrame()
        self.openFileFrame.setLabelText('选择word文件目录：')
        self.openFileFrame.bodyLabel.setFixedWidth(150)
        self.saveFileFrame = LineEditFrame()
        self.saveFileFrame.setLabelText('选择保存pdf文件目录：')
        self.saveFileFrame.bodyLabel.setFixedWidth(150)

        self.contentLabel = CaptionLabel()
        self.contentLabel.setText(
            '''
<html><head/><body><p><span style=" font-family:'宋体','monospace'; font-size:16pt; color:#ff0000;">注意：</span>
<span style=" font-family:'宋体','monospace'; color:#ff8b39;"><br/>
</span><span style=" font-family:'宋体','monospace'; font-size:12pt; color:#000000;">请将需要转换的</span>
<span style=" font-family:'JetBrains Mono','monospace'; font-size:12pt; color:#000000;">word</span>
<span style=" font-family:'宋体','monospace'; font-size:12pt; color:#000000;">文件放到单独的文件夹中，再进行转换。</span>
</p></body></html>
            '''
        )

        self.startButton = PushButton('开始')

        self.vBox.addWidget(self.returnButton)
        self.vBox.addWidget(self.titleLabel)
        self.vBox.addWidget(self.openFileFrame)
        self.vBox.addWidget(self.saveFileFrame)
        self.vBox.addWidget(self.startButton)
        self.vBox.addWidget(self.contentLabel)

        self.startButton.clicked.connect(self.started.emit)
        self.returnButton.clicked.connect(self.returned.emit)

    def resizeEvent(self, a0: QResizeEvent):
        self.stateToolTip.move(a0.size().width() - 10 - self.stateToolTip.width(), 20)

    def setTitle(self, text):
        self.titleLabel.setText(text)
        self.update()

    def setToolTipTitle(self, text):
        self.stateToolTip.setTitle(text)

    def setToolTipContent(self, text):
        self.stateToolTip.setContent(text)

    def setLabelText(self, t1, t2):
        self.openFileFrame.setLabelText(t1)
        self.saveFileFrame.setLabelText(t2)
        self.update()

    def lineEditText(self):
        return self.openFileFrame.lineEditText(), self.saveFileFrame.lineEditText()
